using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Musicman.Business;
using MusicmanWeb.Custom;
using Musicman.Common.Entities;
using System.Collections.Generic;

namespace MusicmanWeb.USER
{
    public partial class PendingDownloads : MusicmanPage
    {
        protected override void Page_Load(object sender, EventArgs e)
        {
            ReddirectNonActiveSession();
            if (!IsPostBack)
            {
                Cache.Remove("pendingdownloads");
                long userId = Int64.Parse(User.Identity.Name);
                List<SearchFile> files = GetPendingDownloads(userId);
                PendingDownloadsGV.DataSource = files;
                PendingDownloadsGV.DataBind();
            }
        }

        private List<SearchFile> GetPendingDownloads(long userID)
        {
            if (Cache["pendingdownloads"] == null)
            {
                List<SearchFile> files = SearchFileManager.Instance.LoadPendingDownloads(userID);
                Cache.Insert("pendingdownloads", files, null,
                    System.Web.Caching.Cache.NoAbsoluteExpiration,
                    new TimeSpan(0, 3, 0));
            }
            return Cache["pendingdownloads"] as List<SearchFile>;
        }

        protected void PendingDownloadsGV_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            MyGridView gv = sender as MyGridView;
            long userId = Int64.Parse(User.Identity.Name);
            List<SearchFile> files = GetPendingDownloads(userId);
            gv.DataSource = files;
            gv.PageIndex = e.NewPageIndex;
            gv.DataBind();
        }

        protected void PendingDownloadsGV_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            SearchFile sf = e.Row.DataItem as SearchFile;
            if (sf != null)
            {
                if (sf.PresentSequence > 0)
                {
                    e.Row.CssClass = "oldfiles";
                }
                else if (sf.PresentSequence == -1)
                {
                    e.Row.CssClass = "newfiles";
                }
                e.Row.Cells[0].Text = Musicman.Common.Utilities.Converter.NameTrimmed(sf.FilePath + "\\" + sf.Name, 30);
                e.Row.Cells[0].ToolTip = sf.FilePath + "\\" + sf.Name;
                e.Row.Cells[1].Text = Musicman.Common.Utilities.Converter.NameTrimmed(sf.Name, 40);
                e.Row.Cells[1].ToolTip = sf.Name;
                e.Row.Cells[2].Text = Musicman.Common.Utilities.Converter.LengthMB(sf.Length);
            }
        }

        protected void RemoveButton_Command(object sender, CommandEventArgs e)
        {
            MyGridView gv = this.PendingDownloadsGV;
            long userid = Int64.Parse(Session["UserID"].ToString());
            List<long> ids = new List<long>();
            foreach (int index in gv.GetSelectedIndices())
            {
                long key = (long)gv.DataKeys[index].Value;
                ids.Add(key);
            }
            long userId = Int64.Parse(User.Identity.Name);
            SearchFileManager.Instance.RemoveFromPendingDownloads(userId, ids);
            Cache.Remove("pendingdownloads");
            List<SearchFile> files = GetPendingDownloads(userId);
            gv.DataSource = files;
            gv.DataBind();
        }
    }
}
